package com.ly.service.user;

import com.google.common.collect.Lists;
import com.ly.base.LoginUserUtil;
import com.ly.entity.Role;
import com.ly.entity.User;
import com.ly.repository.RoleRepository;
import com.ly.repository.UserRepository;
import com.ly.service.IUserService;
import com.ly.service.ServiceResult;
import com.ly.web.dto.UserDTO;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：LY
 * @date ：Created in 2020/11/8 20:04
 * @modified By：
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private ModelMapper modelMapper;

    private final Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();

    @Override
    public User findUserByName(String userName) {
        User user = userRepository.findByName(userName);

        if (user == null) {
            return null;
        }
        //调用dao的方法实现，dao的方法名必须写对，否则将会出现编译错误
        //获取此用户的权限信息
        List<Role> roles = roleRepository.findRolesByUserId(user.getId());
        if (roles == null || roles.isEmpty()) {
            throw new DisabledException("权限非法");
        }

        List<GrantedAuthority> authorities = new ArrayList<>();
        roles.forEach(role -> authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName())));
        user.setAuthorityList(authorities);
        return user;
    }

    @Override
    public ServiceResult<UserDTO> findById(Long adminId) {
        User one = userRepository.findOne(adminId);
        if (one == null){
            return ServiceResult.notFound();
        }
        UserDTO userDTO = modelMapper.map(one,UserDTO.class);

        return ServiceResult.of(userDTO);
    }

    @Override
    public User findUserByTelephone(String telephone) {
       User user = userRepository.findUserByPhoneNumber(telephone);
        if (user == null){
            return null;
        }
       List<Role> roles  =  roleRepository.findRolesByUserId(user.getId());
       if (roles == null|| roles.isEmpty()){
           throw new DisabledException("权限非法");
       }

       List<GrantedAuthority> authorities = new ArrayList<>();
       roles.forEach(role -> authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName())));
       user.setAuthorityList(authorities);
        return user;
    }


    @Override
    @Transactional
    public User addUserByPhone(String telephone) {
        User user = new User();
        user.setPhoneNumber(telephone);
        user.setName(telephone.substring(0,3)+"****"+telephone.substring(7,telephone.length()));
        Date now = new Date();
        user.setCreateTime(now);
        user.setLastLoginTime(now);
        user.setLastUpdateTime(now);
         user = userRepository.save(user);
        Role role = new Role();
        role.setName("USER");
        role.setUserId(user.getId());
        roleRepository.save(role);
        user.setAuthorityList(Lists.newArrayList(new SimpleGrantedAuthority("ROLE_USER")));
        return user;
    }

    @Override
    @Transactional
    public ServiceResult modifyUserProfile(String profile, String value) {
        Long userId = LoginUserUtil.getLoginUserId();
        if (profile == null||profile.isEmpty()){
            return new ServiceResult(false,"属性不可以为空");
        }
        switch (profile){
            case "name":
                userRepository.updateUsername(userId,value);
                break;
            case "email":
                userRepository.updateEmail(userId,value);
                break;
            case "password":
                userRepository.updatePassword(userId,this.passwordEncoder.encodePassword(value,userId));
                break;
                default:
                    return new ServiceResult(false,"不支持的属性");
        }
        return ServiceResult.success();
    }
}
